package com.mls.fivedistrict.controller.backend;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mls.fivedistrict.common.PageFilter;
import com.mls.fivedistrict.common.RequestHolder;
import com.mls.fivedistrict.common.ServiceResponse;
import com.mls.fivedistrict.exception.ParamException;
import com.mls.fivedistrict.pojo.NewsClassify;
import com.mls.fivedistrict.server.NewsClassifyService;
import com.mls.fivedistrict.util.validator.BeanValidator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

@Slf4j
@RestController
@RequestMapping("/api/manager")
public class NewsClassifyController {
    @Autowired
    NewsClassifyService newsClassifyService;

    @GetMapping("/v1/newsClassifies")
    public ServiceResponse getNewsClassifyList() {
        PageFilter pageFilter = RequestHolder.getPageFilter();
        PageHelper.startPage(pageFilter.getPageNum(), pageFilter.getPageSize());
        List newsClassifyList = newsClassifyService.selectList(pageFilter.getWrapper());
        PageInfo pageInfo = new PageInfo<NewsClassify>(newsClassifyList, 5);
        return ServiceResponse.createSuccessResponse(pageInfo);
    }
    @GetMapping("/v1/newsClassify/{newsClassifyId}")
    public  ServiceResponse getNewsClassify(@PathVariable Long newsClassifyId){
        Optional.of(newsClassifyId).orElseThrow(() -> new ParamException("新闻分类id不能为空"));
        NewsClassify newsClassify = newsClassifyService.selectById(newsClassifyId);
        if(null!=newsClassify){
            return  ServiceResponse.createSuccessResponse(newsClassify);
        }else{
            return ServiceResponse.createErrorResponse("查无此分类");
        }
    }
    @PostMapping("/v1/newsClassify")
    public ServiceResponse addNewsClassify(@RequestBody NewsClassify newsClassify) {
        BeanValidator.check(newsClassify);
        boolean flag = newsClassifyService.insert(newsClassify);
        if (flag) {
            return ServiceResponse.createSuccessResponse("新闻分类创建成功");

        } else {
            return ServiceResponse.createErrorResponse("新闻分类创建失败");
        }
    }

    @PutMapping("/v1/newsClassify/{newsClassifyId}")
    public ServiceResponse updateNewsClassify(@PathVariable Long newsClassifyId, @RequestBody NewsClassify newsClassify) {
        Optional.of(newsClassifyId).orElseThrow(() -> new ParamException("新闻分类id不能为空"));
        Optional.of(newsClassify).orElseThrow(() -> new ParamException("更新的内容不能为空"));
        newsClassify.setId(newsClassifyId);
        boolean flag = newsClassifyService.updateById(newsClassify);
        if (flag) {
            return ServiceResponse.createSuccessResponse("编辑成功");

        } else {
            return ServiceResponse.createErrorResponse("编辑失败");
        }
    }

    @DeleteMapping("/v1/newsClassify/{newsClassifyId")
    public ServiceResponse deleteNewsClassifyById(@PathVariable Long newsClassifyId) {
        Optional.of(newsClassifyId).orElseThrow(() -> new ParamException("新闻分类Id不能为空"));
        NewsClassify newsClassify = new NewsClassify();
        newsClassify.setId(newsClassifyId);
        newsClassify.setDelete(true);
        boolean flag = newsClassifyService.updateById(newsClassify);
        if (flag) {
            return ServiceResponse.createSuccessResponse("删除成功");
        } else {
            return ServiceResponse.createErrorResponse("删除失败");
        }
    }

}




